<?php
	namespace app\admin\controller;
	use think\Controller;
	use app\admin\model\Admin as UserModel;
	use app\admin\controller\Base;
	use app\admin\validate\User as UserValidate;
	use app\admin\validate\Adminmsg as Adminmsgdate;
	use app\admin\model\usermsg;
	use app\admin\model\adminmsg;
	use app\admin\model\shopping;
	use app\index\model\person;
	class User extends Base{
		public function index(){
			// $data=usermsg::paginate(3);
			$data=usermsg::all(['type'=>0]);
			$this->assign('data',$data);
			return $this->fetch();
		}
		public function userinfo(){
			$id=input('get.id');
			$data=usermsg::get($id);
			return json($data);
			// return json_encode($data);
		}
		public function addmsg(){
			$id=input('get.id');
			$user = new usermsg();
			// 过滤get数组中的非数据表字段数据
			$ret=$user->allowField(true)->save([
				'type'=>1
			],['id' => $id]);
			if($ret){
				$this->success('允许成功','User/index');
			}else{
				$this->error('操作失败');
			}
		}
		public function delmsg(){
			$id=input('get.id');
			$user = new usermsg();
			// 过滤get数组中的非数据表字段数据
			$ret=$user->allowField(true)->save([
				'type'=>-1
			],['id' => $id]);
			if($ret){
				$this->success('拒绝成功','User/index');
			}else{
				$this->error('操作失败');
			}
		}

		public function zdelmsg(){
			$id=input('get.id');
			$ret=usermsg::destroy($id);
			if($ret){
				$this->success('删除申请成功','User/index');
			}else{
				$this->error('删除申请失败');
			}
		}
		public function showtgmsg(){
			$data=usermsg::all(['type'=>1]);
			$this->assign('data',$data);
			return $this->fetch();
		}
		public function showjjmsg(){
			$data=usermsg::all(['type'=>-1]);
			$this->assign('data',$data);
			return $this->fetch();
		}
		public function addadminmsg(){
			return $this->fetch();
		}
		public function addadminmsgrun(){
			$data=input('post.');
			$val=new Adminmsgdate();
			if(!$val->check($data)){
				$this->error($val->getError());
				exit();
			};
			$data['name']=session('user');
			$data['time']=time(); 
			$admin=new adminmsg($data);
			$ret =$admin->allowField(true)->save();
			if($ret){
				$this->success('新增数据成功','User/index');
			}else{
				$this->error('新增数据失败');
			}
		}
		public function infoadminmsg(){
			return $this->fetch();
		}
		public function deladminmsg(){
			$id=input('get.id');
			$ret=adminmsg::destroy($id);
			if($ret){
				$this->success('删除用户成功','User/index');
			}else{
				$this->error('删除管理员失败');
			}
		}
		public function updateadminmsg(){
			$data=input('post.');
			$adminmsg=new adminmsg();
			$ret=$adminmsg->allowField(true)->save($data,['id'=>$data['id']]);
			if($ret){
				$this->success('修改管理员成功','User/index');
			}else{
				$this->error('修改管理员失败');
			}
		}
		public function tablemsg(){
			$data=adminmsg::all();
			$arrmsg=[
				"code"=>0,
				"msg"=>"",
				"count"=>1000,
				"data"=>$data
			];
			return json($arrmsg);
			// json_encode($arrmsg);
			// return json(
			// 	"code":0,
			// 	"msg":"",
			// 	"count":1000
			// );
		}
		public function userlist(){
			$data = UserModel::all();
     		$this->assign('data', $data);
			$this->assign('page', 1);
     		return $this->fetch();
			//分页
			// $data=UserModel::paginate(3);
			// $page=$data->render();
			// $this->assign('data',$data);
			// $this->assign('page',$page);
			// return $this->fetch();
		}
		public function delete($id){
			$ret=UserModel::destroy($id);
			if($ret){
				$this->success('删除用户成功','User/index');
			}else{
				$this->error('删除管理员失败');
			}
		}
		public function add(){
			return $this->fetch();
		}
		public function edit($id){
			$data=UserModel::get($id);
			$this->assign('data',$data);
			return $this->fetch();
		}
		//新增管理
		public function insert(){
			$data=input('post.');
			$val=new UserValidate();
			if(!$val->check($data)){
				$this->error($val->getError());
				exit();
			}
			$user=new UserModel($data);
			$ret = $user->allowField(true)->save();
			if($ret){
				$this->success('新增管理员成功','User/index');
			}else{
				$this->error('新增管理员失败');
			}
		}
		public function update(){
			$id=input('post.id');
			$data=input('post.');
			// $val=new UserValidate();
			// if(!$val->check($data)){
			// 	$this->error($val->getError());
			// 	exit();
			// }	
			$user=new UserModel();
			$ret=$user->allowField(true)->save($data,['id'=>$id]);
			if($ret){
				$this->success('修改管理员成功','User/index');
			}else{
				$this->error('修改管理员失败');
			}
		}
		public function canvasdata(){
			return $this->fetch();
		}
		public function myData(){
		
			$cg=usermsg::where('type','=',1)->count();
			$all=usermsg::where('type','=',-1)->count();

			return json([
				[
					"sector"=>"申请成功",
					"size"=>$cg
				],
				[
					"sector"=>"申请失败",
					"size"=>$all	
				]
			]);
		}
		//商品
		public function addshopp(){
			return $this->fetch();
		}
		public function submissionmsg(){
				$data=input('post.');
				$file = request()->file('img');
				$result=$this->validate(['file1'=>$file],['file1'=>'require'],['file1.require'=>'请上传格式为100*100的PNG格式文件']);
				if (true !== $result) 
      			{
					$this->error($result['file1']);
				}
				$info = $file->rule(function($file){
					return session('name')."".$file->getInfo('type').date('--m-d_H-i-s');
				})->move(ROOT_PATH.'public/uploads/shopping');
				if($info){
					$shopping=new shopping();
					$shopping->data([
						'title'  =>  $data['title'],
						'text' => $data['text'],
						'price'=>$data['price'],
						'img'=>'/uploads/shopping/'.$info->getSaveName(),
					 ]);
					if($shopping->allowField(true)->save()){
						return "<script>alert('添加成功');window.location.href='index?cancel=yes';</script>";
					}
			}else{
				$this->error("上传出错");
			}
		}
		public function shoppinfo(){
			$id=input('get.id');
			$data=shopping::get($id);
			return json($data);
		}
		public function userdelmsg($id){
			// // 过滤get数组中的非数据表字段数据
			$ret=shopping::destroy($id);
			if($ret){
				$this->success('删除商品成功','User/showshopp');
			}else{
				$this->error('删除商品失败');
			}
		}
		public function useraddmsg($id){
			$data=shopping::get($id);
			$this->assign('data',$data);
			return $this->fetch();
			
		}
		public function showuser(){
			$data = person::all();
			$this->assign('data', $data);
		   $this->assign('page', 1);
			return $this->fetch();
		}
		public function userdelete($id){
			$ret=person::destroy($id);
			if($ret){
				$this->success('删除用户成功','/admin/user/showuser.html');
			}else{
				$this->error('删除用户失败');
			}
		}
		public function useredit($id){
			$data=person::get($id);
			$this->assign('data',$data);
			return $this->fetch();
		}
		public function userupdate(){
			$id=input('post.id');
			$data=input('post.');
			$user=new person();
			$ret=$user->allowField(true)->save($data,['id'=>$id]);
			if($ret){
				$this->success('修改用户成功','/admin/user/showuser.html');
			}else{
				$this->error('修改用户失败');
			}
		}
		public function showshopp(){
			$data=shopping::all();
			$this->assign('data',$data);
			return $this->fetch();
		}
		public function updatashopp(){
			$id=input('post.id');
			$data=input('post.');
			$user=new shopping();
			$ret=$user->allowField(true)->save([
				'title'=>$data['title'],
				'price'=>$data['price'],
				'text'=>$data['text']
			],['id'=>$id]);
			if($ret){
				$this->success('修改商品成功','User/showshopp');
			}else{
				$this->error('修改商品失败');
			}
		}
	}
?>